package com.mayavi.bc.impl;

import java.sql.Timestamp;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.mayavi.bc.IMovieBc;
import com.mayavi.bo.MovieBo;
import com.mayavi.dao.IMovieDao;
import com.mayavi.dto.MovieDto;

public class MovieBcImpl implements IMovieBc{

	private IMovieDao movieDao;
	@Override
	public List<MovieBo> getAllMovies() {
		return movieDao.getAllMovies();
	}

	@Override
	public MovieBo getMovieById(Integer id) {
		return movieDao.getMovieById(id);
	}

	@Override
	public void saveMovie(MovieDto dto) {
		Integer id = dto.getId();
		MovieBo movieBo = null;
		if(id!=null&&id>0){//update
			movieBo = movieDao.getMovieById(id);
		}else {//save
			movieBo = new MovieBo();
			movieBo.setCreationTstamp(new Timestamp(System.currentTimeMillis()));
			movieBo.setCreatedByUserId(dto.getModifiedById());
		}
		movieBo.setLastModifiedByUserId(dto.getModifiedById());
		movieBo.setLastModifiedTstamp(new Timestamp(System.currentTimeMillis()));
		BeanUtils.copyProperties(dto, movieBo);
		movieDao.saveMovie(movieBo);
	}

	public IMovieDao getMovieDao() {
		return movieDao;
	}

	public void setMovieDao(IMovieDao movieDao) {
		this.movieDao = movieDao;
	}

}
